package com.tfjybj.itoo.exam.provider.service.impl;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Reference;;
import com.dmsdbj.itoo.tool.base.service.impl.BaseServicePlusImpl;
import com.dmsdbj.itoo.tool.constantutil.YearSemester;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.CourseEntity;
import com.tfjybj.itoo.exam.model.StaffInstitutionModel;
import com.tfjybj.itoo.exam.provider.dao.CourseDao;
import com.tfjybj.itoo.exam.provider.service.CourseService;
import com.tfjybj.itoo.exam.provider.service.StaffInstitutionService;
import com.tfjybj.itoo.exam.provider.service.TrainingProgramsService;
import com.tfjybj.itoo.teach.FacadeApi.TrainingProgramsApi;
import com.tfjybj.itoo.teach.entity.TrainingProgramsEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * CourseService接口实现类
 * ${base}表
 *
 * @author 杨江涛
 * @version 2.0.0
 * @since 2.0.0 2018-11-24 15:14:12
 */
@Service("courseService")
public class CourseServiceImpl extends BaseServicePlusImpl<CourseDao, CourseEntity> implements CourseService {

    //region 模板生成

    @Resource
    private CourseDao courseDao;

    @DubboReference(check = false)
    private TrainingProgramsApi trainingProgramsApi;

   @Resource
   private TrainingProgramsService trainingProgramsService;

    @Resource
    private StaffInstitutionService staffInstitutionService;

    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * CourseService接口实现类
     * ${base}表
     *
     * @return 当前学年课程信息
     * @author 杨江涛
     * @since 2.0.0 2018-11-24 15:14:12
     */
    @Override
    public List<CourseEntity> findCourse() {
        List<CourseEntity> course = new ArrayList<>();
        List<TrainingProgramsEntity> trainingProgramsEntityList = trainingProgramsApi.findCourseId();
        for (TrainingProgramsEntity trai : trainingProgramsEntityList) {
            course.add(courseDao.findCourse(trai.getCourseId()));
        }
        return course;
    }

    /**
     * CourseService接口实现类
     *
     * @param id 课程id
     * @return 课程信息
     * @author 杨江涛
     * @since 2.0.0 2018-11-24 15:14:12
     */
    @Override
    public String findByCourseId(String id) {
        CourseEntity courseEntity = courseDao.findByCourseId(id);
        if (courseEntity == null) {
            return "";
        } else {
            return courseEntity.getName();
        }
    }

    /**
     * 根据id查询课程名称返回list类型
     *
     * @param id 主键id
     * @return 课程信息
     * @author 杨江涛
     * @since 2.0.0  2018-12-4 17:06:08
     */
    @Override
    public List<CourseEntity> getByCourseId(String id) {
        List<CourseEntity> byCourseId = courseDao.getByCourseId(id);
        return byCourseId;
    }


    /**
     * 根据参数模糊查询课程名称返回list类型
     *
     * @param pageNo     页码
     * @param pageSize   每页条数
     * @param searchInfo 模糊查询条件（课程名称/课程代码）searchInfo
     * @return 根据参数模糊查询课程名称返回list类型
     * @author 杨江涛
     * @since 2.0.0  2018-12-6 14:50:58
     */
    @Override
    public PageInfo<CourseEntity> getCourse(String searchInfo, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        return new PageInfo<>(courseDao.getCourse(searchInfo));
    }

    /**
     * 创建考试查询课程,无参查询
     *
     * @param userId userId
     * @author maying
     * @since 2018-12-8 21:42:00
     */
    @Override
    public List<CourseEntity> inquiryCourse(String userId) {
        List<CourseEntity> courseEntityList = new ArrayList<>();
        List<StaffInstitutionModel> staffInstitutionModelList = staffInstitutionService.queryCollege(userId);
        staffInstitutionModelList.forEach(staffInstitutionModel -> {
            String semester = YearSemester.getYearSemesterCode(0, 0).get(0);
            List<CourseEntity> courseList = courseDao.inquiryCourse(semester, staffInstitutionModel.getInstitutionId());
            courseEntityList.addAll(courseList);
        });
        return courseEntityList;
    }


    /**
     * 课程练习-查询所有属于课程练习的课程
     *
     * @return 是否查询成功
     * @author 贾建清
     * @since 2.0.0 2019-3-24 09:51:24
     */
    @Override
    public List<CourseEntity> queryCourseAll() {
        List<CourseEntity> courseEntityList = courseDao.queryCourseAll();
        return courseEntityList;
    }


}
